package aws;

import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class MergeTwoSticks {

    private int minCost(List<Integer> sticks) {
        if(sticks == null || sticks.size() == 1) {
            return 0;
        }

        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.addAll(sticks);

        int cost = 0;
        while (queue.size() > 1) {
            int merge = queue.poll() + queue.poll();
            cost = cost + merge;
            queue.add(merge);
        }

        System.out.println(cost);
        return cost;
    }

    public static void main(String[] args) {
        Integer []array = new Integer[]{2,4,3};
        List<Integer> list = Arrays.asList(array);

        new MergeTwoSticks().minCost(list);
        new MergeTwoSticks().minCost(Arrays.asList(new Integer[]{1,8,3,5}));
    }
}
